package Controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;
import java.util.ArrayList;

import View.CommentsReportView;
import View.ReportsView;
import Communication.Client;
import Communication.MessageHandle;
import DB.ReportQuery;
import Entity.Customer;
import Entity.Report;


/** This class controls and managed the process that produce reports to the marketing manager */
public class ReportsController implements Serializable {
	private static final long serialVersionUID = 42L;

	/**  Required attributes */
	ReportQuery reportModel;
	ReportsView reportView;
	Client client;
	CommentsReportController commentController;
	static ArrayList<Report> reportsList=new ArrayList<Report>();
	static Report Rpt;


	/**
	 * Constructor calling to listener class that attached to the process 
	 * @param client
	 * @param workerMainMenuController
	 */
	public ReportsController(Client client, WorkerMainMenuController workerMainMenuController) {

		this.client = client;
		reportView =  new ReportsView();
		reportView.showWindow();			
		reportView.addReportsListeners(new ReportsInfoListener(workerMainMenuController));
	}

	/**  Inner Listener class */ 
	class ReportsInfoListener implements ActionListener
	{
		WorkerMainMenuController workerMainController;

		public ReportsInfoListener(WorkerMainMenuController workerMainController){
			this.workerMainController = workerMainController;
			reportModel = new ReportQuery();

		}

		/**  Handle Actions*/
		public void actionPerformed(ActionEvent event) {


			if (event.getSource() == reportView.getCommentBtn())
			{
				/**  if the requierd report is comments report create new controller that manage comments report*/
				commentController=new CommentsReportController( client,ReportsController.this);
				reportView.hideWindow();

			}
			else if(event.getSource() == reportView.getCustomerBtn())
			{  
				try {
					/**
					 * send to server according to complete the process
					 */
					MessageHandle msg = new MessageHandle(null, MessageHandle.CUSTOMER_REPORT, false);
					client.sendToServer(msg);


				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace( );
				}
				//workerMainController.hideWindow();
				reportView.hideWindow();
			}

			else if( event.getSource() == reportView.getBackBtn())	
			{
				workerMainController.showWindow();
				reportView.hideWindow();
			}
		}
	}


	/**
	 * this function gets the result from the server that passed by client
	 * @param msg
	 */
	public static  void updataView(String msg){

		if (msg.compareTo("Success")==0)
		{
			SystemController.displayMessage("Your Report is Ready");
			reportsList.add(Rpt);	
		}
		else if(msg.compareToIgnoreCase("no cutomers")==0)
		{
			SystemController.displayMessage("There are no customers found");	
		}	
	}

	/**
	 *  shows the window
	 */
	public void showWindow(){
		reportView.setVisible(true);
	}
	/**
	 *  hides the window
	 */
	public void hideWindow(){
		reportView.setVisible(false);
	}		
}












